//
//  SISSTenantProgressManageViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/21.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSTenantProgressManageViewController.h"
#import "SISSComboBoxViewProgress.h"
#import "SISSRentProgressManageCell.h"
#import "SISSTenantProgressViewController.h"
#import "SISSJsonRequestNetworkingManager+Progress.h"


static NSString *tenantProgressManageCellReuseID = @"tenantProgressManageCellReuseID";
@interface SISSTenantProgressManageViewController ()<SISSComboBoxViewProgressDelegate,UITableViewDataSource,UITableViewDelegate>

/** tableView */
@property (weak, nonatomic) SISSBaseTableView *tableView;
/* 求租进度模型数组 */
@property (strong, nonatomic) NSArray<SISSTenantProgressModel *> *tenantProgressArr;

/** 区域 */
@property (copy, nonatomic) NSString *region;
/** 街道办 */
@property (copy, nonatomic) NSString *regionDetail;
/** 价格 */
@property (copy, nonatomic) NSString *price;
/** 户型 */
@property (copy, nonatomic) NSString *houseType;
/** 面积 */
@property (copy, nonatomic) NSArray *area;
/** 朝向 */
@property (copy, nonatomic) NSArray *direction;
/** 装修程度 */
@property (copy, nonatomic) NSArray *decoration;
/** 委托方式 */
@property (copy, nonatomic) NSArray *entrustWay;
/** 委托次数 */
@property (copy, nonatomic) NSArray *entrustCount;
/** 进度 */
@property (copy, nonatomic) NSArray *entrustProgress;


@end

@implementation SISSTenantProgressManageViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //    [self requestTenantProgressManageData];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
    
    
    //    [kSISSRefresh prepareFooterRefresh:self.tableView reloadDataBlock:^{
    //        kSISSRefresh.refreshType = SISSRefressTypeReloadMore;
    //        [weakSelf reloadMoreData];
    //    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    [self requestTenantProgressManageData];
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.filterView.comBoBoxView dismissPopView];
}



#pragma mark - SISSComboBoxViewDelegate 筛选
- (void)comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:(NSString *)rootItemTitle title:(NSString *)title {
    
    if ([rootItemTitle isEqualToString:@"租金预算"]) {
        self.price = title;
    } else if ([rootItemTitle isEqualToString:@"户型"]) {
        self.houseType = title;
    }
    [self requestTenantProgressManageData];
}

- (void)comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:(NSString *)rootItemTitle title:(NSString *)title subtitle:(NSString *)subtitle {
    
    self.region = title;
    self.regionDetail = subtitle;
    [self requestTenantProgressManageData];
}

- (void)comboBoxViewDidSelectDisplayTypeFiltersWithTitle:(NSString *)title subtitles:(NSArray *)subtitles {
    
    if ([title isEqualToString:@"委托方式"]) {
        self.entrustWay = @[];
        self.entrustWay = subtitles;
    } else if ([title isEqualToString:@"委托次数"]) {
        self.entrustCount = @[];
        self.entrustCount = subtitles;
    } else if ([title isEqualToString:@"进度"]) {
        self.entrustProgress = @[];
        self.entrustProgress = subtitles;
    } else if ([title isEqualToString:@"面积"]) {
        self.area = @[];
        self.area = subtitles;
    } else if ([title isEqualToString:@"朝向"]) {
        self.direction = @[];
        self.direction = subtitles;
    } else if ([title isEqualToString:@"装修程度"]) {
        self.decoration = @[];
        self.decoration = subtitles;
    }
    
}

- (void)comboBoxViewDisplayTypeFiltersConfirmClickAction {
    
    [self requestTenantProgressManageData];
    
    self.entrustWay = @[];
    self.entrustCount = @[];
    self.entrustProgress = @[];
    self.area = @[];
    self.direction = @[];
    self.decoration = @[];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.tenantProgressArr.count];
    return self.tenantProgressArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSRentProgressManageCell *cell = [tableView dequeueReusableCellWithIdentifier:tenantProgressManageCellReuseID forIndexPath:indexPath];
    cell.tenantProgressModel = self.tenantProgressArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSTenantProgressViewController *vc = [[SISSTenantProgressViewController alloc] init];
    WeakSelf
    vc.deleteCompletedBlock = ^{

        [weakSelf requestTenantProgressManageData];
    };
    vc.title = @"求租进度详情";
    vc.tenantProgressModel = self.tenantProgressArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    
    //    self.pageNo = 1;
    
    [self requestTenantProgressManageData];
    
}

- (void)reloadMoreData {
    
    //    self.pageNo += 1;
    
    [self requestTenantProgressManageData];
}

#pragma mark - 加载数据
- (void)requestTenantProgressManageData {
    
    NSString *region = self.region ? self.region : @"不限";
    NSString *regionDetail = self.regionDetail ? self.regionDetail : @"不限";
    NSString *price = self.price ? self.price : @"";
    NSString *houseType = self.houseType ? self.houseType : @"不限";
    
    NSDictionary *parameters = @{
                                 @"spId":[SISSUserAccount shared].spId,
                                 @"region":region,
                                 @"regionDetail":regionDetail,
                                 @"price":price,
                                 @"houseType":houseType,
                                 @"area":self.area,
                                 @"direction":self.direction,
                                 @"decoration":self.decoration,
//                                 @"entrustWay":self.entrustWay,
                                 @"entrustCount":self.entrustCount,
                                 @"entrustProgress":self.entrustProgress
                                 };
    WeakSelf
    [kJsonRequestNetworkingManager requestFilterTenantProgressWithParameters:parameters success:^(NSArray<SISSTenantProgressModel *> *tenantArr) {

        weakSelf.tenantProgressArr = tenantArr;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }];

    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    SISSComboBoxViewProgress *filterView = [[SISSComboBoxViewProgress alloc] initWithFrame:CGRectMake(0, self.navigationHeight, SCREEN_WIDTH, 44) isHouseResource:NO isRent:YES];
    filterView.delegate = self;
    [self.view addSubview:filterView];
    self.filterView = filterView;
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    // 自动行高
    tableView.estimatedRowHeight = 87;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[SISSRentProgressManageCell class] forCellReuseIdentifier:tenantProgressManageCellReuseID];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(filterView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-kTabBarHeight);
    }];
}

#pragma mark - 懒加载

- (NSArray *)area {
    if (_area == nil) {
        _area = [NSArray array];
    }
    return _area;
}

- (NSArray *)direction {
    if (_direction == nil) {
        _direction = [NSArray array];
    }
    return _direction;
}

- (NSArray *)decoration {
    if (_decoration == nil) {
        _decoration = [NSArray array];
    }
    return _decoration;
}

- (NSArray *)entrustWay {
    if (!_entrustWay) {
        _entrustWay = [NSArray array];
    }
    return _entrustWay;
}

- (NSArray *)entrustCount {
    if (!_entrustCount) {
        _entrustCount = [NSArray array];
    }
    return _entrustCount;
}

- (NSArray *)entrustProgress {
    if (!_entrustProgress) {
        _entrustProgress = [NSArray array];
    }
    return _entrustProgress;
}

@end
